import os
import sys
import json

sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))
from mysql_conncet import ReadFromMysql
from config import *
from base_class.inspect_logging import print_to_log
from aws_model.aws_stepfunc import send_task_success, send_task_failure


def run_sql(event):
    sql = event['sql']
    print_to_log('sql:',sql)
    mysql = ReadFromMysql(host=mysqlhost, user=user, password=password, port=3306)
    mysql.makepool('air_bigdata')
    rs = mysql.read_from_mysql(sql)
    pay_load = {'result': rs}
    output = {"msg": f"${sql} submit ok", "Payload": pay_load}
    output = json.dumps(output)
    # print(output)
    send_task_success(tasktoken, output)
    print_to_log(f"${sql} submit ok")


if __name__ == '__main__':
    try:
        print_to_log(sys.argv[1])
        event = json.loads(sys.argv[1])
        print_to_log('event:',event)
        tasktoken = event.get('TaskToken')
        run_sql(event)
    except Exception as e:
        print_to_log("athena_submit 失败")
        print_to_log("原因： ", str(e))
        send_task_failure(str(e), f'执行{sys._getframe().f_code.co_filename}错误', tasktoken)
